﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Aierp.Entities;
using Aierp.Repository;
using Aierp.SystemEntities.System;
using Aierp.Utility;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Mvc;

namespace Aierp.Controllers
{
    [Route("api/[controller]")]
    public class RolesController : ControllerBase
    {
        IRepository<Sys_Roles> Repository { get; set; }
        SysVisitor SysVisitor { get; set; }
        public RolesController(IRepository<Sys_Roles> repository, SysVisitor sysVisitor)
        {
            Repository = repository;
            SysVisitor = sysVisitor;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task Get()
        {
            var pageParam = this.GetPageParam();
            var ret = Repository.GetList(pageParam.Limit, pageParam.Offset, pageParam.SearchModel, pageParam.Order);
            await this.WriteAsync(ret);
        }
        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/values/5
        [HttpGet("{id}")]
        public async Task Get(int id)
        {
           var model = Repository.FindById(id);
            //return JSONhelper.ToJson(model);
            if (model == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(model);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task GetAll()
        {
            var list = Repository.FindAll();
            //return JSONhelper.ToJson(list);
            if (list == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(list);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        // POST api/values
        [HttpPost]
        public async Task Post(string value)
        {
            //检查用户是否有权限操作
            if (!await CheckUser(SysVisitor)) return;

            ////获取用户对象
            var model = GetModel<Sys_Roles>();

            if (model.RoleName.IsEmpty())
            {
                await this.WriteSpecReasonJson("保存失败，错误信息如下：角色名称不能为空！");
                return;
            } 

            var result = Repository.Add(model);

            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            if (result == -2)
            {
                await this.WriteSpecReasonJson("角色已存在，请更换角色名称！");
                return;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        [HttpPost("Update")]
        public async Task Update()
        {
            //检查用户是否有权限操作
            if (!await CheckUser(SysVisitor)) return;

            //获取用户对象
            var model = GetModel<Sys_Roles>();

            if (model.RoleName.IsEmpty())
            {
                await this.WriteSpecReasonJson("保存失败，错误信息如下：角色名称不能为空！");
                return;
            }

            if (Repository.Update(model) > 0)
            {
                await this.WriteSucessJson();
            }
            else
            {
                await this.WriteFailJson();
            }
        }


        /// <summary>
        /// 停用
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            //检查用户是否有权限操作
            if (!await CheckUser(SysVisitor)) return;

            if (id == 0)
            {
                await this.WriteSpecReasonJson("系统中没有这个角色！");
                return;
            }

            var result = Repository.Remove(id);

            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("系统中找不到这个角色！");
                    return;
                case -8:
                    await this.WriteSpecReasonJson("该角色已经被用户使用，不能删除！");
                    return;
                case -9:
                    await this.WriteSpecReasonJson("管理员角色不能删除！");
                    return;
                default:
                    break;
            }
        }
    }
}
